/*
 * Powered By xTOOLs
 * Development by xtools@xprogrammer.net
 * Copyright © 2009 - 2017 xprogrammer.net. All rights reserved.
 *
 * Generated at 2017年12月26日 09:07:29
 *
 */


package com.pactera.jep.service.sys.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.pactera.jep.autoconfigure.web.controller.BaseRestController;
import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.entity.Treeable;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.sys.model.MetaRegion;
import com.pactera.jep.sys.service.MetaRegionService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.entity.TreeNode;
import com.pactera.jep.web.util.TreeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value="数据区域管理")
@RestController
@RequestMapping("/metaRegion")
public class MetaRegionRestController extends BaseRestController<MetaRegion> {
	private static Logger logger = LoggerFactory.getLogger(MetaRegionRestController.class);
	
	@Autowired
	private MetaRegionService	metaRegionService;

	/**
	 * 移除元数据区域，元数据的分类。节点 
	 * @param regionId
	 * @return
	 */
	@ApiOperation(value = "移除节点")
	@PostMapping(value = "/removeNode/{regionId}")
	public Map<String, Object> removeNode(@PathVariable String regionId) {
		Map<String, Object> tips = new HashMap<String, Object>();
		
		/*查询当前节点是否有子节点*/
		long count = metaRegionService.count(new Filter.Builder().eq("parentId", regionId).build());
		
		/*总数大于0，提示先删除子节点，反之进行删除*/
		if(count > 0)
		{
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.DELETE_SUBNODE_FIRST);
		}
		else
		{
			try
			{
				metaRegionService.deleteByPrimaryKey(regionId);
				tips.put(WebCode.SUCCESS, true);
				tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
			}
			catch(ServiceException e)
			{
				logger.info(e.getMessage());
				tips.put(WebCode.SUCCESS, false);
				tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
			}
		}
		return tips;
	}

	/**
	 * 新增节点
	 * @param metaRegion
	 * @return
	 */
	@ApiOperation(value = "新增节点")
	@PostMapping(value = "/updateNode")
	public Map<String, Object> updateNode(@RequestBody MetaRegion metaRegion) {
		Map<String, Object> tips = new HashMap<String, Object>();
		try
		{
			/*判断当前对象是否存在, 不存在就添加*/
			Boolean exist = metaRegionService.exists(metaRegion.getRegionId());
			if(exist)
			{
				metaRegionService.update(metaRegion);
			}
			else
			{
				metaRegionService.insertMetaRegion(metaRegion);
			}
			
			tips.put(Treeable.TREE_ID, metaRegion.getRegionId());
			tips.put(WebCode.SUCCESS, true);
			tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
		}
		catch(Exception ex)
		{
			tips.put(WebCode.SUCCESS, false);
			tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
		}
		return tips;
	}

	/**
	 * 获取元数据tree节点的值
	 * @return
	 */
	@ApiOperation(value = "获取元数据tree节点的值")
	@GetMapping(value = "/treeNode")
	public List<TreeNode> treeNode(String parentId, Model model, HttpServletRequest request) {

		List<Filter> filters = null;

		if(StringUtils.isBlank(parentId))
		{
			filters = new Filter.Builder().nl("parentId").build();
		}
		else
		{
			filters = new Filter.Builder().eq("parentId", parentId).build();
		}
		
		List<Order> orders = new Order.Builder().asc("disorder").build();
		List<MetaRegion> list = metaRegionService.select(filters, orders);
		
		String[] columns = new String[]{
			"regionId",
			"regionCode",
			"regionName",
			"regionDesc",
			"tablespaceId",
			"tablespaceCode",
			"parentId",
			"pathCode",
			"pathName",
			"status",
			"hierarchy",
			"disorder",
			"tableCount",
		};
		
		List<TreeNode> nodes = TreeUtils.parse(list, columns);
		return nodes;
	}
	
	/**
	 * 验证提交的regionCode是否已经存在数据库中
	 * @param regionCode
	 * @return
	 */
	@RequestMapping(value = "/check", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String, Object> check(String regionCode){
		
		Map<String, Object> tips = new HashMap<String, Object>();
		
		List<Filter> filters = new Filter.Builder().eq("regionCode", regionCode).build();
		Boolean isExist = metaRegionService.exists(filters);
		tips.put("valid", !isExist);
		return tips;
	}
	
	/**
	 * 验证提交的regionCode是否已经存在数据库中
	 * @param  regionCode
	 * @return
	 */
	@RequestMapping(value = "/getMetaRegion", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Map<String, Object> getMetaRegion(String regionId){
		Map<String, Object> tips = new HashMap<String, Object>();
		MetaRegion metaRegion = metaRegionService.get(regionId);
		tips.put("metaRegion", metaRegion);
		return tips;
	}
	
	/**
	 * 根据角色获得其对应的表，把这些表附加到数据区域树上
	 * @return 权限控制主页面
	 */
	@ApiOperation(value = "根据角色获得其对应的表,把这些表附加到数据区域树上")
	@PostMapping(value = "/getRegionTreeByRoleCode")
	public List<TreeNode> getRegionTreeByRoleCode(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
			List<TreeNode> nodes = new ArrayList<TreeNode>();
		/*只有当权限ID不为空的时候才进行查询*/
		String roleCode = jsonObject.getString("roleCode");
		String parentId = jsonObject.getString("parentId");
		if(StringUtils.isNotBlank(roleCode)){
			List<Treeable> regionTreeList = metaRegionService.selectMetaRegionTreeByRoleCodeAndRegionId(roleCode, parentId);
			String[] columns = new String[]{
					"type"
				};
			nodes = TreeUtils.parse(regionTreeList, columns);
		}
		
		return nodes;
	}
}